// 复制文本到系统粘贴板，返回true或者false，表示是否复制成功
export function copyText (str) {
    const input = document.createElement('textarea')
    document.body.appendChild(input)
    input.value = str
    input.select()
    if (document.execCommand('copy')) {
        document.execCommand('copy')
        document.body.removeChild(input)
        return true
    } else {
        document.body.removeChild(input)
        return false
    }
}

// 时间格式化
export function formatTime (date, format = 'yyyy-MM-dd hh:mm:ss') {
    const o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds(),
        'q+': Math.floor((date.getMonth() + 3) / 3),
        S: date.getMilliseconds()
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }

    for (const k in o) {
        if (new RegExp('(' + k + ')').test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
        }
    }
    return format
}

/**
 * 模拟生成uuid值
 * @returns {string}
 */
export function uuid () {
    let str1 = Math.random().toString(32)
    let str2 = Math.random().toString(32)
    if (str1.length > 3) {
        str1 = str1.slice(2)
    }
    if (str2.length > 3) {
        str2 = str2.slice(2)
    }
    return str1 + str2
}

export function addJsAndCss (fileUrl, fileId) {
    if (fileUrl.endsWith('.js')) {
        const dom = document.createElement('script')
        dom.setAttribute('type', 'text/javascript')
        dom.setAttribute('src', fileUrl)
        dom.setAttribute('id', fileId)
        document.head.appendChild(dom)
    } else if (fileUrl.endsWith('.css')) {
        const dom = document.createElement('link')
        dom.setAttribute('rel', 'stylesheet')
        dom.setAttribute('href', fileUrl)
        dom.setAttribute('id', fileId)
        document.head.appendChild(dom)
    }
}

export function addHighlight () {
    if (!window.hljs) {
        if (!document.querySelector('cesium-css')) {
            addJsAndCss('https://cdn.bootcdn.net/ajax/libs/highlight.js/11.11.1/styles/github-dark.min.css', 'hljs-css')
        }
        if (!document.querySelector('cesium-js')) {
            addJsAndCss('https://cdn.bootcdn.net/ajax/libs/highlight.js/11.11.1/highlight.min.js', 'hljs-js')
        }
        return new Promise((resolve) => {
            const timer = setInterval(() => {
                if (window.hljs) {
                    clearInterval(timer)
                    resolve(true)
                }
            }, 200)
        })
    }
}


export function addCesium () {
    if (!window.Cesium) {
        if (!document.querySelector('cesium-css')) {
            addJsAndCss('https://cdn.bootcdn.net/ajax/libs/cesium/1.125.0/Widgets/widgets.css', 'cesium-css')
        }
        if (!document.querySelector('cesium-js')) {
            addJsAndCss('https://cdn.bootcdn.net/ajax/libs/cesium/1.125.0/Cesium.min.js', 'cesium-js')
        }
        return new Promise((resolve) => {
            const timer = setInterval(() => {
                if (window.Cesium) {
                    clearInterval(timer)
                    resolve(true)
                }
            }, 200)
        })
    }
}

export function addLeaflet () {
    if (!window.L) {
        if (!document.querySelector('leaflet-css')) {
            addJsAndCss('https://cdn.bootcdn.net/ajax/libs/leaflet/1.9.4/leaflet.min.css', 'leaflet-css')
        }
        if (!document.querySelector('leaflet-js')) {
            addJsAndCss('https://cdn.bootcdn.net/ajax/libs/leaflet/1.9.4/leaflet.min.js', 'leaflet-js')
        }
        return new Promise((resolve) => {
            const timer = setInterval(() => {
                if (window.L) {
                    clearInterval(timer)
                    resolve(true)
                }
            }, 200)
        })
    }
}

export function addMathJax () {
    if (!window.MathJax) {
        if (!document.querySelector('MathJax-script')) {
            addJsAndCss('https://cdn.bootcdn.net/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js', 'MathJax-script')
        }
        return new Promise((resolve) => {
            const timer = setInterval(() => {
                if (window.MathJax) {
                    clearInterval(timer)
                    resolve(true)
                }
            }, 200)
        })
    }
}

// export function addFFmpeg () {
//     if (!window.FFmpegWASM) {
//         if (!document.querySelector('FFmpeg-script')) {
//             addJsAndCss('./assets/ffmpeg.min.js', 'FFmpeg-script')
//         }
//         return new Promise((resolve) => {
//             const timer = setInterval(() => {
//                 if (window.FFmpegWASM) {
//                     clearInterval(timer)
//                     resolve(true)
//                 }
//             }, 200)
//         })
//     }
// }